

package net.lastcoder.ifserver.nio;

import net.lastcoder.ifserver.packet.Packet;
import net.lastcoder.ifserver.session.Session;

import org.apache.mina.core.buffer.IoBuffer;

import java.io.Closeable;
import java.net.UnknownHostException;

/**
 * ifserver的所有长连接，必须继承这个接口
 * @author WenYF
 */
public interface Connection  {
    /**
     * @return 链接是否还有效
     */
    boolean validate();

    /**
     * 设置对应的会话，必须设置
     *
     * @param session the session that owns this connection
     */
    void init(Session session);

    /**
     * @return  对饮 {@link #init(Session)}
     */
    Session getOwner();
    
    /**
     * @return  ip地址.
     * @throws UnknownHostException 
     */
    byte[] getAddress() throws UnknownHostException;

    String getHostAddress() throws UnknownHostException;

    String getHostName() throws UnknownHostException;

    /**
     * 将会回调通知close，查看{@link #registerCloseListener}
     */
    void close();

    /**
     * @return 是否已关闭
     */
    boolean isClosed();

    void systemShutdown();
    void registerCloseListener(ConnectionCloseListener listener, Object handbackMessage);

    void removeCloseListener(ConnectionCloseListener listener);

    void deliver(Packet packet);

    void deliverRawData(byte[] data);

    void deliverBuffer(IoBuffer buffer);

    void deliverRawData(byte[] data, int off, int len);
    
    enum State { OPEN, CLOSED }

}
